****************************************************************************************************************************
Date: 13 JAN 2015

Purpose: Add parental diagnoses

****************************************************************************************************************************;


****************************************************************************************************************************
											 Tilføjer hjerte-sygdomme;
****************************************************************************************************************************

*Tager en kopi af diagnosevariablerne (aktionsdiagnoser fra den psykiske register indeholder ikke DJ og DI diagnoser,
så derfor tager jeg ikke en kopi af den)
De "originale" variable bruger jeg til at vælge de relevante diagnoser udfra de to første bogstaver.
Kopien bruger jeg til at lave dummies (k=kopi);


libname in1 'K:\Data\Workdata\702727\ADHD_grunddata\Datasæt';

data Hjerte;
set in1.alle_diag;

k_adiag=adiag;
k_diag=diag;
k_grundmorbus=grundmorbus;
k_bidiagnose_1=bidiagnose_1;
k_bidiagnose_2=bidiagnose_2;
k_bidiagnose_3=bidiagnose_3;
k_bidiagnose_4=bidiagnose_4;
k_bidiagnose_5=bidiagnose_5;
k_bidiagnose_6=bidiagnose_6;
k_bidiagnose_7=bidiagnose_7;
k_bidiagnose_8=bidiagnose_8;
k_bidiagnose_9=bidiagnose_9;
k_bidiagnose_10=bidiagnose_10;
k_bidiagnose_11=bidiagnose_11;
k_bidiagnose_12=bidiagnose_12;
k_bidiagnose_13=bidiagnose_13;
k_bidiagnose_14=bidiagnose_14;
k1_hoveddiagnose=hoveddiagnose;
k1_adiag=adiag;
k1_diag=diag;
run;


data Hjerte;

length adiag $ 2;
length diag $ 2;
length grundmorbus $2;
length bidiagnose_1 $2;
length bidiagnose_2 $2;
length bidiagnose_3 $2;
length bidiagnose_4 $2;
length bidiagnose_5 $2;
length bidiagnose_6 $2;
length bidiagnose_7 $2;
length bidiagnose_8 $2;
length bidiagnose_9 $2;
length bidiagnose_10 $2;
length bidiagnose_11 $2;
length bidiagnose_12 $2;
length bidiagnose_13 $2;
length bidiagnose_14 $2;
length k_adiag$4;
length k_diag $4;
length k_grundmorbus $4;
length k_bidiagnose_1 $4;
length k_bidiagnose_2 $4;
length k_bidiagnose_3 $4;
length k_bidiagnose_4 $4;
length k_bidiagnose_5 $4;
length k_bidiagnose_6 $4;
length k_bidiagnose_7 $4;
length k_bidiagnose_8 $4;
length k_bidiagnose_9 $4;
length k_bidiagnose_10 $4;
length k_bidiagnose_11 $4;
length k_bidiagnose_12 $4;
length k_bidiagnose_13 $4;
length k_bidiagnose_14 $4;


length k1_adiag$5;
length k1_diag $5;
length k1_hoveddiagnose $5;

set Hjerte;

run;

*Koder først hjertesygdomme fra før 1994 (ICD-8);
*Laver diagnoserne om til numeriske værdier;

data Hjerte;
set Hjerte;
k1_adiag=compress(k1_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_diag=compress(k1_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_hoveddiagnose=compress(k1_hoveddiagnose,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");

run;


data Hjerte;
set Hjerte;

n_adiag=input(k1_adiag,5.);
n_diag=input(k1_diag,5.);
n_hoveddiagnose=input(k1_hoveddiagnose,5.);
run;

data Hjerte;
set Hjerte;


*Kredsløbssygdomme;
if 39000<=n_adiag<=44799 then CIRC_diag=1;
if 39000<=n_diag<=44799   then CIRC_diag=1;
if 39000<=n_hoveddiagnose<=44799 then CIRC_diag=1;

if 45099<=n_adiag<=45900 and n_adiag^=45691 and n_adiag^=45701 then CIRC_diag=1;
if 45099<=n_diag<=45900 and n_diag^=45691 and n_diag^=45701  then CIRC_diag=1;
if 45099<=n_hoveddiagnose<=45900 and n_hoveddiagnose^=45691 and n_hoveddiagnose^=45701  then CIRC_diag=1;
run;

*Udvælger hjertesygdomme (psyk_diag indeholder ikke DJ eller DI diagnoser, derfor er den ikke medtaget);

data Hjerte;
set Hjerte;

if diag="DI" or adiag="DI" or grundmorbus="DI" or bidiagnose_1="DI" or bidiagnose_2="DI" or bidiagnose_3="DI" or bidiagnose_4="DI" or bidiagnose_5="DI"
or bidiagnose_6="DI" or bidiagnose_7="DI" or bidiagnose_8="DI" or bidiagnose_9="DI" or bidiagnose_10="DI" or bidiagnose_11="DI" 
or bidiagnose_12="DI" or bidiagnose_13="DI" or bidiagnose_14="DI" 
or CIRC_diag=1 
then output Hjerte;

run;


*Bytter rundt på rækkefølgen af variable;

data hjerte;

retain pnr recnum sgh hospital diagtype adiag diag n_adiag n_diag n_hoveddiagnose dato_diag dato_psyk year_diag month_diag day_diag psyk_adiag year_psyk month_psyk day_psyk 
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

nk_adiag nk_diag nk_grundmorbus nk_bidiagnose_1 nk_bidiagnose_2 nk_bidiagnose_3 nk_bidiagnose_4 nk_bidiagnose_5 nk_bidiagnose_6 
nk_bidiagnose_7 nk_bidiagnose_8 nk_bidiagnose_9 nk_bidiagnose_10 nk_bidiagnose_11 nk_bidiagnose_12 nk_bidiagnose_13 nk_bidiagnose_14
tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52  tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53
CIRC_diag;
set hjerte;
run;

****************************************************
OBS:

Betragter nu kun observationer med hjertesygdomme
Enten fra ICD-8 eller ICD-10
****************************************************;

* Alle i denne sample har min en gang modtage en circulatory diagnosis; 

data Hjerte1;
set Hjerte;

CIRC_diag=1;
run;




*Jeg fjerner de variable jeg ikke skal bruge;

data Hjerte2;
set Hjerte1;
drop 
recnum sgh hospital diagtype adiag diag  hoveddiagnose psyk_adiag
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

n_adiag n_diag n_hoveddiagnose nk_adiag nk_diag nk_grundmorbus nk_bidiagnose_1 nk_bidiagnose_2 nk_bidiagnose_3 nk_bidiagnose_4 nk_bidiagnose_5 nk_bidiagnose_6 
nk_bidiagnose_7 nk_bidiagnose_8 nk_bidiagnose_9 nk_bidiagnose_10 nk_bidiagnose_11 nk_bidiagnose_12 nk_bidiagnose_13 nk_bidiagnose_14
tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52 tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53

k_adiag k_diag k_grundmorbus k_bidiagnose_1 k_bidiagnose_2 k_bidiagnose_3 k_bidiagnose_4 k_bidiagnose_5 k_bidiagnose_6 k_bidiagnose_7
k_bidiagnose_8 k_bidiagnose_9 k_bidiagnose_10 k_bidiagnose_11 k_bidiagnose_12 k_bidiagnose_13 k_bidiagnose_14

k1_adiag k1_diag k1_hoveddiagnose

year_diag month_diag day_diag year_psyk month_psyk day_psyk
;

run;

**********************************************************************************
Diagnose datoen er lige nu afhængig af om diagnosen er stillet i lpr er psyk reg.
	Laver nu en samlet diagnose dato
**********************************************************************************;
data Hjerte2;
set Hjerte2;

If dato_psyk="." then CIRC_date=dato_diag;
If dato_diag="." then CIRC_date=dato_psyk;
run;


****************************************************************
Omdøber variable så de kan matches med forældre i mit grunddata
****************************************************************;

data Hjerte_mor;
set Hjerte2;

drop dato_psyk dato_diag;
rename pnr=pnrm;
rename CIRC_diag=CIRC_diag_mom;
rename CIRC_date=CIRC_date_mom;
run;
data Hjerte_mor;
set Hjerte_mor;
label pnrm="Personnummer - mor";
run;

data Hjerte_far;
set Hjerte2;

drop dato_psyk dato_diag;
rename pnr=pnrf;
rename CIRC_diag=CIRC_diag_dad;
rename CIRC_date=CIRC_date_dad;
run;
data Hjerte_far;
set Hjerte_far;
label pnrf="Personnummer - far";
run;

proc sort data=Hjerte_mor;
by pnrm;
run;
proc sort data=Hjerte_far;
by pnrf;
run;


*****************************************************************************
Indhenter data om barnets fødselsdato

Må tilføje diagnoser for hvert enkelt af moderens/faderen børn
	-Opdele child data i mors forskellige børn
	- tilføje diagnoser til enkelte barn
*****************************************************************************;

libname in2 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in2.nicu_childhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;

*Opdeler sample i moderens børn;
proc sort data=child;
by pnrm birthyear;
run;

data child;
set child;
d1=1;
by pnrm;
if first.pnrm then d2=d1;
else d2+d1;
run;

proc freq data=child;
table d2;
run;

*Opdeler sample i moderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child ;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


***************************************************************************
Merger nu hjertediagnoser på hver enkel sample med hvert enkelt barn.

- På en måde kan jeg få et mål for diagnosetiming efter for hvert enkelt barn.
	Uanset birthorder mm. 

****************************************************************;

%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;
*Mor; 
data hjertemor_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) Hjerte_mor;
by pnrm;
if a;
run;

data hjertemor_barn&i;
set hjertemor_barn&i;

temp_CIRCdiag_mom_before=0;
temp_CIRCdiag_mom_after=0;

if CIRC_diag_mom=1 and CIRC_date_mom<FODTDATO then temp_CIRCdiag_mom_before=1;		/*Diagnose før barnet fødes*/
if CIRC_diag_mom=1 and CIRC_date_mom>FODTDATO and CIRC_date_mom<=FODTDATO+5400 then temp_CIRCdiag_mom_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=hjertemor_barn&i;
by pnrm descending temp_CIRCdiag_mom_before;
run;

data hjertemor_barn&i;
set hjertemor_barn&i;
by pnrm;
if first.pnrm then temp2_CIRCdiag_mom_before=temp_CIRCdiag_mom_before;
else temp2_CIRCdiag_mom_before+temp_CIRCdiag_mom_before; 
run;


proc sort data=hjertemor_barn&i;
by pnrm descending temp_CIRCdiag_mom_after;
run;

data hjertemor_barn&i;
set hjertemor_barn&i;
by pnrm;
if first.pnrm then temp2_CIRCdiag_mom_after=temp_CIRCdiag_mom_after;
else temp2_CIRCdiag_mom_after+temp_CIRCdiag_mom_after; 
run;


data hjertemor_barn&i;
set hjertemor_barn&i;

CIRCdiag_mom_before=0;
if temp2_CIRCdiag_mom_before>0 then CIRCdiag_mom_before=1; 

CIRCdiag_mom_after=0;
if temp2_CIRCdiag_mom_after>0 then CIRCdiag_mom_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=hjertemor_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_circmor_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data circmor_barn&i;
set temp_circmor_barn&i (drop=CIRC_date_mom temp_CIRCdiag_mom_before temp2_CIRCdiag_mom_before temp_CIRCdiag_mom_after temp2_CIRCdiag_mom_after _TYPE_ _FREQ_ CIRC_diag_mom);
run;

%end; 
%mend;
%barn(1,8);
run;


proc datasets;
delete hjertemor_barn1-hjertemor_barn8;
quit; 
proc datasets;
delete temp_circmor_barn1-temp_circmor_barn8;
quit; 

proc datasets;
delete barn1-barn8;
quit; 


*********************************

Gør nu tilsvarende for far

*********************************; 

*Opdeler sample i faderens børn;
proc sort data=child;
by pnrf birthyear;
run;

data child_far;
set child;
d1=1;
by pnrf;
if first.pnrf then d2=d1;
else d2+d1;
run;

proc freq data=child_far;
table d2;
run;

*Opdeler sample i faderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child_far;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;

data hjertefar_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) Hjerte_far;
by pnrf;
if a;
run;

data hjertefar_barn&i;
set hjertefar_barn&i;

temp_CIRCdiag_dad_before=0;
temp_CIRCdiag_dad_after=0;

if CIRC_diag_dad=1 and CIRC_date_dad<FODTDATO then temp_CIRCdiag_dad_before=1;		/*Diagnose før barnet fødes*/
if CIRC_diag_dad=1 and CIRC_date_dad>FODTDATO and CIRC_date_dad<=FODTDATO+5400 then temp_CIRCdiag_dad_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=hjertefar_barn&i;
by pnrf descending temp_CIRCdiag_dad_before;
run;

data hjertefar_barn&i;
set hjertefar_barn&i;
by pnrf;
if first.pnrf then temp2_CIRCdiag_dad_before=temp_CIRCdiag_dad_before;
else temp2_CIRCdiag_dad_before+temp_CIRCdiag_dad_before; 
run;

proc sort data=hjertefar_barn&i;
by pnrf descending temp_CIRCdiag_dad_after;
run;

data hjertefar_barn&i;
set hjertefar_barn&i;
by pnrf;
if first.pnrf then temp2_CIRCdiag_dad_after=temp_CIRCdiag_dad_after;
else temp2_CIRCdiag_dad_after+temp_CIRCdiag_dad_after; 
run;


data hjertefar_barn&i;
set hjertefar_barn&i;

CIRCdiag_dad_before=0;
if temp2_CIRCdiag_dad_before>0 then CIRCdiag_dad_before=1; 

CIRCdiag_dad_after=0;
if temp2_CIRCdiag_dad_after>0 then CIRCdiag_dad_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=hjertefar_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_circfar_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data circfar_barn&i;
set temp_circfar_barn&i (drop=CIRC_date_dad temp_CIRCdiag_dad_before temp2_CIRCdiag_dad_before temp_CIRCdiag_dad_after temp2_CIRCdiag_dad_after _TYPE_ _FREQ_ CIRC_diag_dad);
run;

%end; 
%mend;
%barn(1,8);
run;


proc datasets;
delete hjertefar_barn1-hjertefar_barn8;
quit; 
proc datasets;
delete temp_circfar_barn1-temp_circfar_barn8;
quit; 
proc datasets;
delete barn1-barn8;
quit; 


**************************************************************************
Samler nu i ét datasæt for hhv mor og far
**************************************************************************;
data circmor;
set circmor_barn1-circmor_barn8;
run;

proc sort data=circmor;
by pnr;
run;

data circfar;
set circfar_barn1-circfar_barn8;
run;

proc sort data=circfar;
by pnr;
run;


*******************************************
Kan nu merge dette på mit fulde datasæt
*******************************************;

proc sort data= child (drop= birthyear d1 d2);
by pnr;
run;
*mor;
data sample_circ;
merge child (in=a) circmor;
by pnr;
if a;
run;
*far;
data sample_circ;
merge sample_circ (in=b) circfar;
by pnr;
if b;
run;

proc freq data=sample_circ;
table CIRCdiag_mom_before CIRCdiag_mom_after CIRCdiag_dad_before CIRCdiag_dad_after;
run;


proc datasets;
delete circfar_barn1-circfar_barn8;
quit; 
proc datasets;
delete circmor_barn1-circmor_barn8;
quit; 



*******************************************************************************************************************************

Kan nu gå videre og tilføjer diagnoser vedrørende det respiratoriske system

*******************************************************************************************************************************;
libname in2 'K:\Data\Workdata\702727\ADHD_grunddata\Datasæt';

data respiratoriske;
set in2.alle_diag;

k1_adiag=adiag;
k1_diag=diag;
k1_hoveddiagnose=hoveddiagnose;

run;


data respiratoriske;

length adiag $ 2;
length diag $ 2;
length grundmorbus $2;
length bidiagnose_1 $2;
length bidiagnose_2 $2;
length bidiagnose_3 $2;
length bidiagnose_4 $2;
length bidiagnose_5 $2;
length bidiagnose_6 $2;
length bidiagnose_7 $2;
length bidiagnose_8 $2;
length bidiagnose_9 $2;
length bidiagnose_10 $2;
length bidiagnose_11 $2;
length bidiagnose_12 $2;
length bidiagnose_13 $2;
length bidiagnose_14 $2;

length k1_adiag$5;
length k1_diag $5;
length k1_hoveddiagnose $5;

set respiratoriske;

run;


*Koder først lungesygdomme fra før 1994 (ICD-8);
*Laver diagnoserne om til numeriske værdier;

data respiratoriske;
set respiratoriske;
k1_adiag=compress(k1_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_diag=compress(k1_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k1_hoveddiagnose=compress(k1_hoveddiagnose,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");

run;

data respiratoriske;
set respiratoriske;

n_adiag=input(k1_adiag,5.);
n_diag=input(k1_diag,5.);
n_hoveddiagnose=input(k1_hoveddiagnose,5.);

run;

data resp_icd8;
set respiratoriske;


*Åndedrætsdiagnoser;
if n_adiag=45691 then RESP_diag=1;
if n_diag=45691 then RESP_diag=1;
if n_hoveddiagnose=45691 then RESP_diag=1;

if n_adiag=45701 then RESP_diag=1;
if n_diag=45701 then RESP_diag=1;
if n_hoveddiagnose=45701 then RESP_diag=1;

if 46099<=n_adiag<=51999 then RESP_diag=1;
if 46099<=n_diag<=51999 then RESP_diag=1;
if 46099<=n_hoveddiagnose<=51999 then RESP_diag=1;


run;


*Udvælger diagnoser vedrørende det respiratoriske system;

data resp_icd10;
set resp_icd8;

if diag="DJ" or adiag="DJ" or grundmorbus="DJ" or bidiagnose_1="DJ" or bidiagnose_2="DJ" or bidiagnose_3="DJ" or bidiagnose_4="DJ" 
or bidiagnose_5="DJ" or bidiagnose_6="DJ" or bidiagnose_7="DJ" or bidiagnose_8="DJ" or bidiagnose_9="DJ" or bidiagnose_10="DJ" 
or bidiagnose_11="DJ" or bidiagnose_12="DJ" or bidiagnose_13="DJ" or bidiagnose_14="DJ" 

or RESP_diag=1
then output resp_icd10;

run;

data resp;
set resp_icd10;

RESP_diag=1;

run;


*Jeg fjerner de variable jeg ikke skal bruge;

data resp1;
set resp;
drop 
recnum sgh hospital diagtype adiag diag  hoveddiagnose psyk_adiag
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

n_adiag n_diag n_hoveddiagnose
tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52 tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53

k1_adiag k1_diag k1_hoveddiagnose

year_diag month_diag day_diag year_psyk month_psyk day_psyk
;

run;

**********************************************************************************
Diagnose datoen er lige nu afhængig af om diagnosen er stillet i lpr er psyk reg.
	Laver nu en samlet diagnose dato
**********************************************************************************;
data resp1;
set resp1;

If RESP_diag=1 & dato_psyk=(".") then RESP_date=dato_diag;
If RESP_diag=1 & dato_diag=(".") then RESP_date=dato_psyk;

run;


****************************************************************
Omdøber variable så de kan matches med forældre i mit grunddata
****************************************************************;

data Resp_mor;
set resp1;

drop dato_psyk dato_diag;
rename pnr=pnrm;
rename RESP_diag=RESP_diag_mom;
rename RESP_date=RESP_date_mom;
run;
data Resp_mor;
set Resp_mor;
label pnrm="Personnummer - mor";
run;

data Resp_far;
set resp1;

drop dato_psyk dato_diag;
rename pnr=pnrf;
rename RESP_diag=RESP_diag_dad;
rename RESP_date=RESP_date_dad;
run;
data Resp_far;
set Resp_far;
label pnrf="Personnummer - far";
run;

proc sort data=Resp_mor;
by pnrm;
run;
proc sort data=Resp_far;
by pnrf;
run;


*Sletter datasæt jeg ikke skal bruge mere;
proc datasets;
delete child child_far;
quit; 


*****************************************************************************
Indhenter data om barnets fødselsdato

Må tilføje diagnoser for hvert enkelt af moderens/faderen børn
	-Opdele child data i mors forskellige børn
	- tilføje diagnoser til enkelte barn
*****************************************************************************;

libname in3 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in3.nicu_childhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;

*Opdeler sample i moderens børn;
proc sort data=child;
by pnrm birthyear;
run;

data child;
set child;
d1=1;
by pnrm;
if first.pnrm then d2=d1;
else d2+d1;
run;

proc freq data=child;
table d2;
run;

*Opdeler sample i moderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child ;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


***************************************************************************
Merger nu Respiratoriskediagnoser på hver enkel sample med hvert enkelt barn.

- På en måde kan jeg få et mål for diagnosetiming efter for hvert enkelt barn.
	Uanset birthorder mm. 

****************************************************************;

%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;
*Mor; 
data respmor_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) Resp_mor;
by pnrm;
if a;
run;

data Respmor_barn&i;
set Respmor_barn&i;

temp_RESPdiag_mom_before=0;
temp_RESPdiag_mom_after=0;

if RESP_diag_mom=1 and RESP_date_mom<FODTDATO then temp_RESPdiag_mom_before=1;		/*Diagnose før barnet fødes*/
if RESP_diag_mom=1 and RESP_date_mom>FODTDATO and RESP_date_mom<=FODTDATO+5400 then temp_RESPdiag_mom_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=Respmor_barn&i;
by pnrm descending temp_RESPdiag_mom_before;
run;

data Respmor_barn&i;
set Respmor_barn&i;
by pnrm;
if first.pnrm then temp2_RESPdiag_mom_before=temp_RESPdiag_mom_before;
else temp2_RESPdiag_mom_before+temp_RESPdiag_mom_before; 
run;

proc sort data=Respmor_barn&i;
by pnrm descending temp_RESPdiag_mom_after;
run;

data Respmor_barn&i;
set Respmor_barn&i;
by pnrm;
if first.pnrm then temp2_RESPdiag_mom_after=temp_RESPdiag_mom_after;
else temp2_RESPdiag_mom_after+temp_RESPdiag_mom_after; 
run;


data Respmor_barn&i;
set Respmor_barn&i;

RESPdiag_mom_before=0;
if temp2_RESPdiag_mom_before>0 then RESPdiag_mom_before=1; 

RESPdiag_mom_after=0;
if temp2_RESPdiag_mom_after>0 then RESPdiag_mom_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=Respmor_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_Respmor_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data Respirmor_barn&i;
set temp_Respmor_barn&i (drop=RESP_date_mom temp_RESPdiag_mom_before temp2_RESPdiag_mom_before temp_RESPdiag_mom_after temp2_RESPdiag_mom_after _TYPE_ _FREQ_ RESP_diag_mom);
run;

%end; 
%mend;
%barn(1,8);
run;


proc datasets;
delete temp_Respmor_barn1-temp_Respmor_barn8;
quit; 

proc datasets;
delete barn1-barn8;
quit; 

proc datasets;
delete Respmor_barn1-Respmor_barn8;
quit; 


*********************************

Gør nu tilsvarende for far

*********************************; 

*Opdeler sample i faderens børn;
proc sort data=child;
by pnrf birthyear;
run;

data child_far;
set child;
d1=1;
by pnrf;
if first.pnrf then d2=d1;
else d2+d1;
run;

proc freq data=child_far;
table d2;
run;

*Opdeler sample i faderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child_far;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;

data Respfar_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) Resp_far;
by pnrf;
if a;
run;

data Respfar_barn&i;
set Respfar_barn&i;

temp_RESPdiag_dad_before=0;
temp_RESPdiag_dad_after=0;

if RESP_diag_dad=1 and RESP_date_dad<FODTDATO then temp_RESPdiag_dad_before=1;		/*Diagnose før barnet fødes*/
if RESP_diag_dad=1 and RESP_date_dad>FODTDATO and RESP_date_dad<=FODTDATO+5400 then temp_RESPdiag_dad_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=Respfar_barn&i;
by pnrf descending temp_RESPdiag_dad_before;
run;

data Respfar_barn&i;
set Respfar_barn&i;
by pnrf;
if first.pnrf then temp2_RESPdiag_dad_before=temp_RESPdiag_dad_before;
else temp2_RESPdiag_dad_before+temp_RESPdiag_dad_before; 
run;

proc sort data=Respfar_barn&i;
by pnrf descending temp_RESPdiag_dad_after;
run;

data Respfar_barn&i;
set Respfar_barn&i;
by pnrf;
if first.pnrf then temp2_RESPdiag_dad_after=temp_RESPdiag_dad_after;
else temp2_RESPdiag_dad_after+temp_RESPdiag_dad_after; 
run;


data Respfar_barn&i;
set Respfar_barn&i;

RESPdiag_dad_before=0;
if temp2_RESPdiag_dad_before>0 then RESPdiag_dad_before=1; 

RESPdiag_dad_after=0;
if temp2_RESPdiag_dad_after>0 then RESPdiag_dad_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=Respfar_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_Respfar_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data Respirfar_barn&i;
set temp_Respfar_barn&i (drop=RESP_date_dad temp_RESPdiag_dad_before temp2_RESPdiag_dad_before temp_RESPdiag_dad_after temp2_RESPdiag_dad_after _TYPE_ _FREQ_ RESP_diag_dad);
run;

%end; 
%mend;
%barn(1,8);
run;

proc datasets;
delete temp_Respfar_barn1-temp_Respfar_barn8;
quit; 
proc datasets;
delete barn1-barn8;
quit; 
proc datasets;
delete Respfar_barn1-Respfar_barn8;
quit; 

**************************************************************************
Samler nu i ét datasæt for hhv mor og far
**************************************************************************;
data respmor;
set Respirmor_barn1-Respirmor_barn8;
run;

proc sort data=respmor;
by pnr;
run;

data respfar;
set Respirfar_barn1-Respirfar_barn8;
run;

proc sort data=respfar;
by pnr;
run;

proc datasets;
delete Respirfar_barn1-Respirfar_barn8;
quit; 

proc datasets;
delete Respirmor_barn1-Respirmor_barn8;
quit;


*******************************************
Kan nu merge dette på mit fulde datasæt
*******************************************;

proc sort data= child (drop= birthyear d1 d2);
by pnr;
run;
*mor;
data sample_resp;
merge child (in=a) respmor;
by pnr;
if a;
run;
*far;
data sample_resp;
merge sample_resp (in=b) respfar;
by pnr;
if b;
run;

proc freq data=sample_resp;
table RESPdiag_mom_before RESPdiag_mom_after RESPdiag_dad_before RESPdiag_dad_after;
run;


*******************************************************************************************************************************

Kan nu gå videre og tilføjer psykiatriske diagnoser

*******************************************************************************************************************************;
libname in3 'K:\Data\Workdata\702727\ADHD_grunddata\Datasæt';

data psykiske;
set in3.alle_diag;

k1_adiag=adiag;
k1_diag=diag;
k1_psyk_adiag=psyk_adiag;
k1_hoveddiagnose=hoveddiagnose;
k1_grundmorbus=grundmorbus;
k1_bidiagnose_1=bidiagnose_1;
k1_bidiagnose_2=bidiagnose_2;
k1_bidiagnose_3=bidiagnose_3;
k1_bidiagnose_4=bidiagnose_4;
k1_bidiagnose_5=bidiagnose_5;
k1_bidiagnose_6=bidiagnose_6;
k1_bidiagnose_7=bidiagnose_7;
k1_bidiagnose_8=bidiagnose_8;
k1_bidiagnose_9=bidiagnose_9;
k1_bidiagnose_10=bidiagnose_10;
k1_bidiagnose_11=bidiagnose_11;
k1_bidiagnose_12=bidiagnose_12;
k1_bidiagnose_13=bidiagnose_13;
k1_bidiagnose_14=bidiagnose_14;

k2_adiag=adiag;
k2_diag=diag;
k2_psyk_adiag=psyk_adiag;
k2_hoveddiagnose=hoveddiagnose;
k2_grundmorbus=grundmorbus;
k2_bidiagnose_1=bidiagnose_1;
k2_bidiagnose_2=bidiagnose_2;
k2_bidiagnose_3=bidiagnose_3;
k2_bidiagnose_4=bidiagnose_4;
k2_bidiagnose_5=bidiagnose_5;
k2_bidiagnose_6=bidiagnose_6;
k2_bidiagnose_7=bidiagnose_7;
k2_bidiagnose_8=bidiagnose_8;
k2_bidiagnose_9=bidiagnose_9;
k2_bidiagnose_10=bidiagnose_10;
k2_bidiagnose_11=bidiagnose_11;
k2_bidiagnose_12=bidiagnose_12;
k2_bidiagnose_13=bidiagnose_13;
k2_bidiagnose_14=bidiagnose_14;

run;


data psykiske;

length adiag $ 2;
length diag $ 2;
length psyk_adiag $2;
length grundmorbus $2;
length bidiagnose_1 $2;
length bidiagnose_2 $2;
length bidiagnose_3 $2;
length bidiagnose_4 $2;
length bidiagnose_5 $2;
length bidiagnose_6 $2;
length bidiagnose_7 $2;
length bidiagnose_8 $2;
length bidiagnose_9 $2;
length bidiagnose_10 $2;
length bidiagnose_11 $2;
length bidiagnose_12 $2;
length bidiagnose_13 $2;
length bidiagnose_14 $2;


length k1_adiag$4;
length k1_diag $4;
length k1_psyk_adiag $4;
length k1_grundmorbus $4;
length k1_bidiagnose_1 $4;
length k1_bidiagnose_2 $4;
length k1_bidiagnose_3 $4;
length k1_bidiagnose_4 $4;
length k1_bidiagnose_5 $4;
length k1_bidiagnose_6 $4;
length k1_bidiagnose_7 $4;
length k1_bidiagnose_8 $4;
length k1_bidiagnose_9 $4;
length k1_bidiagnose_10 $4;
length k1_bidiagnose_11 $4;
length k1_bidiagnose_12 $4;
length k1_bidiagnose_13 $4;
length k1_bidiagnose_14 $4;

length k2_adiag$5;
length k2_diag $5;
length k2_psyk_adiag $5;
length k2_hoveddiagnose $5;
length k2_grundmorbus $5;
length k2_bidiagnose_1 $5;
length k2_bidiagnose_2 $5;
length k2_bidiagnose_3 $5;
length k2_bidiagnose_4 $5;
length k2_bidiagnose_5 $5;
length k2_bidiagnose_6 $5;
length k2_bidiagnose_7 $5;
length k2_bidiagnose_8 $5;
length k2_bidiagnose_9 $5;
length k2_bidiagnose_10 $5;
length k2_bidiagnose_11 $5;
length k2_bidiagnose_12 $5;
length k2_bidiagnose_13 $5;
length k2_bidiagnose_14 $5;

set psykiske;

run;



*Koder først psykiske diagnoser fra før 1994 (ICD-8);
*Laver diagnoserne om til numeriske værdier;

data psykiske;
set psykiske;
k2_adiag=compress(k2_adiag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_diag=compress(k2_diag,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");
k2_hoveddiagnose=compress(k2_hoveddiagnose,"ABCDFEFGHIJKLMNOPQRSTUVWXYZÆØÅ");

run;


data psykiske;
set psykiske;

n_adiag=input(k2_adiag,5.);
n_diag=input(k2_diag,5.);
n_hoveddiagnose=input(k2_hoveddiagnose,5.);

run;

data psykiske_icd8;
set psykiske;

*024 Sindsygdomme og 025 Mentale sygdomme;
if 29009<=n_adiag<=31599 then PSYCH_diag=1;
if 29009<=n_diag<=31599 then PSYCH_diag=1;
if 29009<=n_hoveddiagnose<=31599 then PSYCH_diag=1;


run;

*Udvælger psykiske diagnoser;

data psykiske_icd10;
set psykiske_icd8;

if diag="DF" or adiag="DF" or psyk_adiag="DF" or grundmorbus="DF" or bidiagnose_1="DF" or bidiagnose_2="DF" or bidiagnose_3="DF" 
or bidiagnose_4="DF" or bidiagnose_5="DF" or bidiagnose_6="DF" or bidiagnose_7="DF" or bidiagnose_8="DF" or bidiagnose_9="DF"
or bidiagnose_10="DF" or bidiagnose_11="DF" or bidiagnose_12="DF" or bidiagnose_13="DF" or bidiagnose_14="DF" 
or PSYCH_diag=1
then output psykiske_icd10;

run;


data PSYCH;
set psykiske_icd10;

PSYCH_diag=1;

run;


*Jeg fjerner de variable jeg ikke skal bruge;

data PSYCH1;
set PSYCH;
drop 
recnum sgh hospital diagtype adiag diag  hoveddiagnose psyk_adiag
grundmorbus bidiagnose_1 bidiagnose_2 bidiagnose_3 bidiagnose_4 bidiagnose_5 bidiagnose_6 bidiagnose_7 bidiagnose_8
bidiagnose_9 bidiagnose_10 bidiagnose_11 bidiagnose_12 bidiagnose_13 bidiagnose_14

n_adiag n_diag n_hoveddiagnose
tillagskodeA1 tillagskodeA2 tillagskodeA3 tillagskodeG1 tillagskodeG2 tillagskodeG3
tillagskodeB11 tillagskodeB12 tillagskodeB13 tillagskodeB21 tillagskodeB22 tillagskodeB23
tillagskodeB31 tillagskodeB32 tillagskodeB33 tillagskodeB41 tillagskodeB42 tillagskodeB43
tillagskodeB51 tillagskodeB52 tillagskodeB61 tillagskodeB62 tillagskodeB63 tillagskodeG53

k1_adiag k1_diag k1_hoveddiagnose

k1_psyk_adiag k1_grundmorbus k1_bidiagnose_1 k1_bidiagnose_2 k1_bidiagnose_3 k1_bidiagnose_4 k1_bidiagnose_5 k1_bidiagnose_6 k1_bidiagnose_7
k1_bidiagnose_8 k1_bidiagnose_9 k1_bidiagnose_10 k1_bidiagnose_11 k1_bidiagnose_12 k1_bidiagnose_13 k1_bidiagnose_14

k2_adiag k2_diag k2_psyk_adiag k2_hoveddiagnose k2_grundmorbus k2_bidiagnose_1 k2_bidiagnose_2 k2_bidiagnose_3 k2_bidiagnose_4 k2_bidiagnose_5
k2_bidiagnose_6 k2_bidiagnose_7 k2_bidiagnose_8 k2_bidiagnose_9 k2_bidiagnose_10 k2_bidiagnose_11 k2_bidiagnose_12 k2_bidiagnose_13 k2_bidiagnose_14

year_diag month_diag day_diag year_psyk month_psyk day_psyk
;

run;

**********************************************************************************
Diagnose datoen er lige nu afhængig af om diagnosen er stillet i lpr er psyk reg.
	Laver nu en samlet diagnose dato
**********************************************************************************;
data PSYCH1;
set PSYCH1;

If PSYCH_diag=1 & dato_psyk=(".") then PSYCH_date=dato_diag;
If PSYCH_diag=1 & dato_diag=(".") then PSYCH_date=dato_psyk;

run;


****************************************************************
Omdøber variable så de kan matches med forældre i mit grunddata
****************************************************************;

data PSYCH_mor;
set PSYCH1;

drop dato_psyk dato_diag;
rename pnr=pnrm;
rename PSYCH_diag=PSYCH_diag_mom;
rename PSYCH_date=PSYCH_date_mom;
run;
data PSYCH_mor;
set PSYCH_mor;
label pnrm="Personnummer - mor";
run;

data PSYCH_far;
set PSYCH1;

drop dato_psyk dato_diag;
rename pnr=pnrf;
rename PSYCH_diag=PSYCH_diag_dad;
rename PSYCH_date=PSYCH_date_dad;
run;
data PSYCH_far;
set PSYCH_far;
label pnrf="Personnummer - far";
run;

proc sort data=PSYCH_mor;
by pnrm;
run;
proc sort data=PSYCH_far;
by pnrf;
run;


*Sletter datasæt jeg ikke skal bruge mere;
proc datasets;
delete child child_far;
quit; 




*****************************************************************************
Indhenter data om barnets fødselsdato

Må tilføje diagnoser for hvert enkelt af moderens/faderen børn
	-Opdele child data i mors forskellige børn
	- tilføje diagnoser til enkelte barn
*****************************************************************************;

libname in4 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data child;
set in4.nicu_childhealth (keep=pnr pnrm pnrf fodtdato birthyear);
run;

*Opdeler sample i moderens børn;
proc sort data=child;
by pnrm birthyear;
run;

data child;
set child;
d1=1;
by pnrm;
if first.pnrm then d2=d1;
else d2+d1;
run;

proc freq data=child;
table d2;
run;

*Opdeler sample i moderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child ;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


******************************************************************************
Merger nu Psychiatriske diagnoser på hver enkel sample med hvert enkelt barn.

- På en måde kan jeg få et mål for diagnosetiming efter for hvert enkelt barn.
	Uanset birthorder mm. 

*******************************************************************************;

%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;
*Mor; 
data PSYCHmor_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) PSYCH_mor;
by pnrm;
if a;
run;

data PSYCHmor_barn&i;
set PSYCHmor_barn&i;

temp_PSYCHdiag_mom_before=0;
temp_PSYCHdiag_mom_after=0;

if PSYCH_diag_mom=1 and PSYCH_date_mom<FODTDATO then temp_PSYCHdiag_mom_before=1;		/*Diagnose før barnet fødes*/
if PSYCH_diag_mom=1 and PSYCH_date_mom>FODTDATO and PSYCH_date_mom<=FODTDATO+5400 then temp_PSYCHdiag_mom_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=PSYCHmor_barn&i;
by pnrm descending temp_PSYCHdiag_mom_before;
run;

data PSYCHmor_barn&i;
set PSYCHmor_barn&i;
by pnrm;
if first.pnrm then temp2_PSYCHdiag_mom_before=temp_PSYCHdiag_mom_before;
else temp2_PSYCHdiag_mom_before+temp_PSYCHdiag_mom_before; 
run;

proc sort data=PSYCHmor_barn&i;
by pnrm descending temp_PSYCHdiag_mom_after;
run;

data PSYCHmor_barn&i;
set PSYCHmor_barn&i;
by pnrm;
if first.pnrm then temp2_PSYCHdiag_mom_after=temp_PSYCHdiag_mom_after;
else temp2_PSYCHdiag_mom_after+temp_PSYCHdiag_mom_after; 
run;


data PSYCHmor_barn&i;
set PSYCHmor_barn&i;

PSYCHdiag_mom_before=0;
if temp2_PSYCHdiag_mom_before>0 then PSYCHdiag_mom_before=1; 

PSYCHdiag_mom_after=0;
if temp2_PSYCHdiag_mom_after>0 then PSYCHdiag_mom_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=PSYCHmor_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_PSYCHmor_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data PSYCHImor_barn&i;
set temp_PSYCHmor_barn&i (drop=PSYCH_date_mom temp_PSYCHdiag_mom_before temp2_PSYCHdiag_mom_before temp_PSYCHdiag_mom_after temp2_PSYCHdiag_mom_after _TYPE_ _FREQ_ PSYCH_diag_mom);
run;

%end; 
%mend;
%barn(1,8);
run;


proc datasets;
delete temp_PSYCHmor_barn1-temp_PSYCHmor_barn8;
quit; 

proc datasets;
delete barn1-barn8;
quit; 

proc datasets;
delete PSYCHmor_barn1-PSYCHmor_barn8;
quit; 


*********************************

Gør nu tilsvarende for far

*********************************; 

*Opdeler sample i faderens børn;
proc sort data=child;
by pnrf birthyear;
run;

data child_far;
set child;
d1=1;
by pnrf;
if first.pnrf then d2=d1;
else d2+d1;
run;

proc freq data=child_far;
table d2;
run;

*Opdeler sample i faderens børn; 
%macro born(start,slut);
%do i=&start %to &slut;

data barn&i;
set child_far;
if d2=&i;
run;

%end;
%mend;
%born(1,8);


%macro barn(start,slut);
%do i=&start %to &slut;

**********************
*Merger med hoveddata
**********************;

data PSYCHfar_barn&i (drop=d1 d2 birthyear);
merge barn&i (in=a) PSYCH_far;
by pnrf;
if a;
run;

data PSYCHfar_barn&i;
set PSYCHfar_barn&i;

temp_PSYCHdiag_dad_before=0;
temp_PSYCHdiag_dad_after=0;

if PSYCH_diag_dad=1 and PSYCH_date_dad<FODTDATO then temp_PSYCHdiag_dad_before=1;		/*Diagnose før barnet fødes*/
if PSYCH_diag_dad=1 and PSYCH_date_dad>FODTDATO and PSYCH_date_dad<=FODTDATO+5400 then temp_PSYCHdiag_dad_after=1;	/*Diagnose efter barn fødes, men indenfor 15 år*/

run;


proc sort data=PSYCHfar_barn&i;
by pnrf descending temp_PSYCHdiag_dad_before;
run;

data PSYCHfar_barn&i;
set PSYCHfar_barn&i;
by pnrf;
if first.pnrf then temp2_PSYCHdiag_dad_before=temp_PSYCHdiag_dad_before;
else temp2_PSYCHdiag_dad_before+temp_PSYCHdiag_dad_before; 
run;

proc sort data=PSYCHfar_barn&i;
by pnrf descending temp_PSYCHdiag_dad_after;
run;

data PSYCHfar_barn&i;
set PSYCHfar_barn&i;
by pnrf;
if first.pnrf then temp2_PSYCHdiag_dad_after=temp_PSYCHdiag_dad_after;
else temp2_PSYCHdiag_dad_after+temp_PSYCHdiag_dad_after; 
run;


data PSYCHfar_barn&i;
set PSYCHfar_barn&i;

PSYCHdiag_dad_before=0;
if temp2_PSYCHdiag_dad_before>0 then PSYCHdiag_dad_before=1; 

PSYCHdiag_dad_after=0;
if temp2_PSYCHdiag_dad_after>0 then PSYCHdiag_dad_after=1;
run;

***************************
Kollapser data
***************************;

proc means data=PSYCHfar_barn&i NWAY NOPRINT;
class pnr;
Output Out=temp_PSYCHfar_barn&i min=;
run;

*Gemmer kun de variable jeg skal bruge videre;
data PSYCHIfar_barn&i;
set temp_PSYCHfar_barn&i (drop=PSYCH_date_dad temp_PSYCHdiag_dad_before temp2_PSYCHdiag_dad_before temp_PSYCHdiag_dad_after temp2_PSYCHdiag_dad_after _TYPE_ _FREQ_ PSYCH_diag_dad);
run;

%end; 
%mend;
%barn(1,8);
run;

proc datasets;
delete temp_PSYCHfar_barn1-temp_PSYCHfar_barn8;
quit; 
proc datasets;
delete barn1-barn8;
quit; 
proc datasets;
delete PSYCHfar_barn1-PSYCHfar_barn8;
quit; 

**************************************************************************
Samler nu i ét datasæt for hhv mor og far
**************************************************************************;
data PSYCHmor;
set PSYCHImor_barn1-PSYCHImor_barn8;
run;

proc sort data=PSYCHmor;
by pnr;
run;

data PSYCHfar;
set PSYCHIfar_barn1-PSYCHIfar_barn8;
run;

proc sort data=PSYCHfar;
by pnr;
run;

proc datasets;
delete PSYCHIfar_barn1-PSYCHIfar_barn8;
quit; 

proc datasets;
delete PSYCHImor_barn1-PSYCHImor_barn8;
quit;


*******************************************
Kan nu merge dette på mit fulde datasæt
*******************************************;

proc sort data= child (drop= birthyear d1 d2);
by pnr;
run;
*mor;
data sample_PSYCH;
merge child (in=a) PSYCHmor;
by pnr;
if a;
run;
*far;
data sample_PSYCH;
merge sample_PSYCH (in=b) PSYCHfar;
by pnr;
if b;
run;

proc freq data=sample_PSYCH;
table PSYCHdiag_mom_before PSYCHdiag_mom_after PSYCHdiag_dad_before PSYCHdiag_dad_after;
run;


**************************************
Har nu de tre typer af diagnoser
-Sætter missing lig nul.
-Merger efterfølgende på hoved data
***************************************;

data sample_circ;
set sample_circ;

if CIRCdiag_mom_before=. then CIRCdiag_mom_before=0;  
if CIRCdiag_mom_after=. then CIRCdiag_mom_after=0;  

if CIRCdiag_dad_before=. then CIRCdiag_dad_before=0;  
if CIRCdiag_dad_after=. then CIRCdiag_dad_after=0;  

proc sort;
by pnr;
run;


data sample_resp;
set sample_resp;

if RESPdiag_mom_before=. then RESPdiag_mom_before=0;  
if RESPdiag_mom_after=. then RESPdiag_mom_after=0;  

if RESPdiag_dad_before=. then RESPdiag_dad_before=0;  
if RESPdiag_dad_after=. then RESPdiag_dad_after=0;

proc sort;
by pnr; 
run;

data sample_PSYCH;
set sample_PSYCH;

if PSYCHdiag_mom_before=. then PSYCHdiag_mom_before=0;  
if PSYCHdiag_mom_after=. then PSYCHdiag_mom_after=0;  

if PSYCHdiag_dad_before=. then PSYCHdiag_dad_before=0;  
if PSYCHdiag_dad_after=. then PSYCHdiag_dad_after=0;

proc sort;
by pnr; 
run;


*Merger med hoveddata;

libname in4 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data nicu;
set in4.nicu_childhealth;
run;

*Add circ;
data nicu_1;
merge nicu (in=a) sample_circ;
by pnr;
if a;
run;

*Add resp;
data nicu_2;
merge nicu_1 (in=a) sample_resp;
by pnr;
if a;
run;

*Add psych;
data nicu_parentalhealth;
merge nicu_2 (in=a) sample_psych;
by pnr;
if a;
run;


*************
Gemmer data
*************;

*SAS datasæt;
libname out 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data out.nicu_parentalhealth;
set nicu_parentalhealth;
run;
